package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Api(description = "统计用户和访客")
@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    @ApiOperation(value = "跳转到统计数据的页面接口")
    @RequestMapping(value = "/data", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDataPage(){
        return "/site/admin/data";
    }

    @ApiOperation(value = "统计独立访客接口")
    @PostMapping("/data/uv")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "start", value = "统计的开始时间"),
        @ApiImplicitParam(name = "end", value = "统计的结束时间")
    })
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                        Model model){

        long uv = dataService.calculateUV(start, end);

        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "forward:/data";//此处是转发，跳转路径后仍然是一个请求（与reDirect有区别）
    }

    @ApiOperation(value = "统计日活跃用户接口")
    @PostMapping("/data/dau")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "start", value = "统计的开始时间"),
        @ApiImplicitParam(name = "end", value = "统计的结束时间")
    })
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                        Model model){

        long dau = dataService.calculateDAU(start, end);

        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/data";//此处是转发，跳转路径后仍然是一个请求（与reDirect有区别）
    }


}
